package com.lesama.method;

// final void setDaemon(boolean on) 设置为守护线程.当主线程结束时，所有守护线程都会被自动停止，无论它们是否完成任务

public class ThreadMethodDemo2 {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    System.out.println(Thread.currentThread().getName() + "线程执行了" + i);
                }
            }
        }, "A");
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 202; i++) {
                    System.out.println(Thread.currentThread().getName() + "线程执行了" + i);
                }
            }
        }, "B");
        t2.setDaemon(true); // 设置t2为守护线程
        t1.start();
        t2.start(); // t1线程执行完毕后，t2线程也随之终止，无论其是否执行完毕
    }
}
